var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;
var updateList;

var systemDictionListFuntion = function () {
    $.ajax({
        url:"systemRole/findAllRoleByTypePage.json",
        type:'GET',
        dataType:"json",
        data:{
            roleType:1,page:page,pageNum:pageNum
        },
        success:function(result){
            updateList = result.roleList;
            var tem = "<tr>" +
                "<th width='5%'><label><input id='checkAll' type='checkbox' /></label></th>" +
                "<th width='11%'><span>序号</span></th>" +
                "<th width='13%'><span>编码</span></th>" +
                "<th width='30%'><span>角色名称</span></th>" +
                "<th width='40%'><span>角色备注</span></th>" +
                "</tr>";
            $.each( result.roleList, function(index, content){
                tem += "<tr>" +
                    "<td><label><input type='checkbox' name='subBox' class='selectDiv' value='"+content.id+"'/></label></td>" +
                    "<td><span class='tdSpan'>"+(index+1)+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.code+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.roleName+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.remark+"</span></td>" +
                    "</tr>";
            });
            $("#systemDictionTable").empty();
            $("#systemDictionTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageI = 1;
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemDictionPage").empty();
            $("#systemDictionPage").append(temPage);

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });

            $(".selectDiv").click(function(){
                dianjixuanzekuang();
            });

            //复选框
            $("table tr th label,table tr td label").click(function(){
                $(this).toggleClass("on");
            })

            //全选
            $("#checkAll").click(function(){
                if(this.checked){
                    $("td label").addClass("on");
                    $("td :checkbox[name='subBox']").prop("checked", true);
                }else{
                    $("td label").removeClass("on");
                    $("td :checkbox[name='subBox']").prop("checked", false);
                }
            });

            var dianjixuanzekuang = function() {
                if ($('.selectDiv').length == $('.selectDiv:checked').length) {
                    $('#checkAll').prop({
                        checked : true
                    });
                    $("th label").addClass("on");
                } else {
                    $('#checkAll').prop({
                        checked : false
                    });
                    $("th label").removeClass("on");
                }
            }

        }
    });
}

//获取所有复选框选中返回选中的值
var checkboxValueFunction = function(){
    var str="";
    $('input[name="subBox"]:checked').each(function(index, content){
        if(index == $('input[name="subBox"]:checked').size() - 1){
            str+=$(this).val();
        }else {
            str+=$(this).val()+",";
        }
    })
    return str;
}

//新建角色
$(".newEm").click(function(){
    if($('input[name="subBox"]:checked').size() > 1){
        hintsPValueFucntion("请选择一条记录操作");
    }else {
        $(".deskDiv").css({
            display:"block",height:$(document).height()
        });
        $(".roleNew,.teamDiv").css({
            top:($(window).height()-$(".roleNew,.teamDiv").height())/2+$(window).scrollTop()+"px",
            display:"block"
        });
        if($('input[name="subBox"]:checked').size() == 0){
            //新增
            $("#systemDictionAddUpdate")[0].reset();
        }else{
            //修改
            if(checkboxValueFunction()==1 || checkboxValueFunction() == 2 || checkboxValueFunction() == 3 || checkboxValueFunction() == 4 || checkboxValueFunction() == 5){
                hintsPValueFucntion("此角色为系统角色，不允许修改");
            }else {
                $.each( updateList, function(index, content){
                    if(content.id == checkboxValueFunction()){
                        $("input[name='code']").val(content.code);
                        $("input[name='roleName']").val(content.roleName);
                        $("textarea[name='remark']").val(content.remark);
                        $("input[name='id']").val(content.id);
                        $("input[name='type']").val(content.type);
                    }
                });
            }
        }
    }
});

//表单提交
$("#systemDictionSubmit").click(function(){
    $.ajax({
        url:"systemRole/insertRole.json",
        type:'POST',
        dataType:"json",
        data:$("#systemDictionAddUpdate").serialize(),
        success:function(result){
            navPills('systemDictionHtml');
            hintsPValueFucntion(result.msg);
        },
        error:function(){
            hintsPValueFucntion("请求失败");
        }
    });
});

//删除角色
$(".deleteEm").click(function(){
    if($('input[name="subBox"]:checked').size() != 1){
        hintsPValueFucntion("请选择一条记录操作");
    }else {
        $(".deskDiv").css({
            display:"block",height:$(document).height()
        });
        $(".roleDele").css({
            top:($(window).height()-$(".roleDele").height())/2+$(window).scrollTop()+"px",
            display:"block"
        });
    }
});

//删除角色操作
$("#systemDictionDelete").click(function(){
    if(checkboxValueFunction()==1 || checkboxValueFunction() == 2 || checkboxValueFunction() == 3 || checkboxValueFunction() == 4 || checkboxValueFunction() == 5){
        hintsPValueFucntion("此角色为系统角色，不允许删除");
    }else {
        $.ajax({
            url:"systemRole/deleteRole.json",
            type:'POST',
            dataType:"json",
            data:{
                roleId:checkboxValueFunction()
            },
            success:function(result){
                navPills('systemDictionHtml');
                hintsPValueFucntion(result.msg);
            },
            error:function(){
                hintsPValueFucntion("请求失败");
            }
        });
    }
});

//系统权限授权
$(".permissionDiv").click(function(){
    if($('input[name="subBox"]:checked').size() != 1){
        hintsPValueFucntion("请选择一条记录操作");
    }else{
        $(".deskDiv").css({
            display:"block",height:$(document).height()
        });
        $(".authorityResc").css({
            top:($(window).height()-$(".authority").height())/2+$(window).scrollTop()+"px",
            display:"block"
        });
        $("[name='checkbox']").removeAttr("checked");//取消全选
        $("input[name='roleId']").val(checkboxValueFunction());
        systemRescRoleFunction();
    }
});

//业务权限授权
$(".operation").click(function(){
    if($('input[name="subBox"]:checked').size() != 1){
        hintsPValueFucntion("请选择一条记录操作");
    }else{
        $(".deskDiv").css({
            display:"block",height:$(document).height()
        });
        $(".authority").css({
            top:($(window).height()-$(".authority").height())/2+$(window).scrollTop()+"px",
            display:"block"
        });
        $("[name='checkbox']").removeAttr("checked");//取消全选
        $("input[name='roleId']").val(checkboxValueFunction());
        systemOpenCityFunction();
    }
});


//关闭
$(".cancel,.confirm").click(function(){
    closeDivFunction();
});

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}

systemDictionListFuntion();

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemDictionListFuntion();
    pageOld = o;
}

//获取当前账号拥有的系统功能
var systemRescRoleFunction = function(){
    $.ajax({
        url:"usersWeb/findAllRescByRoleIdFromUserId.json",
        type:'POST',
        dataType:"json",
        data:{
            roleId:$("input[name='roleId']").val()
        },
        success:function(result){
            var tem = "";
            $.each( result.rescList, function(index, content){
                if(content.userresc == 0){
                    tem += "<label><input type='checkbox' name='rescBox' class='selectDiv' value='"+content.id+"'/>"+content.name+"</label>";
                }else {
                    tem += "<label class='cur'><input type='checkbox' name='rescBox' class='selectDiv' checked='checked' value='"+content.id+"'/>"+content.name+"</label>";
                }
            });
            $("#systemDictionResc").empty();
            $("#systemDictionResc").append(tem);

            $(".authP label").click(function(){
                $(this).toggleClass("cur");
            });
        },
        error:function(){
            hintsPValueFucntion("请求失败");
        }
    });
}

//保存
$("#systemDictionRescSubmit").click(function(){
    if($('input[name="rescBox"]:checked').size() > 0){
        var rescBox = $('input[name="rescBox"]:checked');
        var rescBoxs = '';
        $.each(rescBox, function(index, content){
            rescBoxs += content.defaultValue + ',';
        });
        rescBoxs = rescBoxs.substr(0,rescBoxs.length-1);
        $.ajax({
            url:"usersWeb/insertIntoRescRole.json",
            type:'POST',
            dataType:"json",
            data:{
                roleId:$("input[name='roleId']").val(),
                rescs:rescBoxs
            },
            success:function(result){
                if(result.isOK == 'OK'){
                    closeDivFunction();
                    navPills('systemDictionHtml');
                    hintsPValueFucntion("操作成功");
                }
            },
            error:function(){
                hintsPValueFucntion("请求失败");
            }
        });
    }else {
        navPills('systemDictionHtml');
    }
});

var closeDivFunction = function (){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".authorityResc").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
}